//
//  ProductDetailViewController.m
//  AppMua5
//
//  Created by duong nguyen on 9/20/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ProductDetailViewController.h"

@interface ProductDetailViewController ()

@end

@implementation ProductDetailViewController
@synthesize muaApi,scrollView,productObject,imageData;

-(id) init{
    [super init];
    muaApi=[[MuaAPI alloc]init];
    scrollView=[[ProductDetailScrollView alloc]init];
    self.productObject=[[ProductObject alloc]init];
    imageData=[[NSData alloc]init];
    return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(void) initIphoneViews{
    // Init scroll view content
    [scrollView setFrame:CGRectMake(0, -5, 320, 379)];
    [scrollView setContentSize:CGSizeMake(320, 1000)];
    [self.view addSubview:scrollView];
}

-(void) loadView{
    [super loadView];
    // Init views
    [self initIphoneViews];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    // Push data to views
    [self pushDataToViews];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    muaApi=nil;
    scrollView=nil;
    productObject=nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma Get data
-(void) setProductObject:(ProductObject *)_productObject{
    productObject=_productObject;
}

-(void) getProductInfo:(NSInteger)userId productId:(NSInteger)productId{
    [muaApi getProductInfo:userId productId:productId context:self];
}
-(void) requestFinished:(ASIHTTPRequest *)request{
    NSString *response=[request responseString];
    [muaApi parseProductInfo:response ShopObject:nil];
}

-(void) requestFailed:(ASIHTTPRequest *)request{

}

#pragma Push data
-(void) pushDataToViews{
//    [scrollView.productSummary setText:productObject.productSummary];
    [scrollView.viewGroupShop setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"shop_detail.png"]]];
    [scrollView.productName setText:[productObject.productName uppercaseString]];
    
    //
    imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString: productObject.productImage]];
    
    if(imageData){
        UIImage *img=[UIImage imageWithData:imageData];
        [scrollView.productImage setImage:img];
 
    }else {
        
    }
}

-(void) dealloc{
    [productObject release];
    [scrollView release];
    [muaApi release];
    [super dealloc];
}
@end
